package com.cneport.transfer.router.support;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cneport.transfer.router.NodeFinder;
import com.cneport.transfer.router.model.Node;
import com.cneport.transfer.router.support.service.NodeService;
import com.cneport.transfer.router.support.service.NodeRelService;

@Service
public class NodeFinderImpl implements NodeFinder {

    @Autowired
    private NodeRelService routerService;

    @Autowired
    private NodeService nodeService;


    public List<Node> findNextNode(String nodeId) {
        List<String> nodeIdList = routerService.selectRouter(nodeId);
        if (nodeIdList.size() <= 0) {
            return null;
        } else {
            List<Node> nodeList = new ArrayList<Node>();
            for (String Rnodeid : nodeIdList) {
                Node node = new Node();
                node = nodeService.getNode(Rnodeid);
                if (node != null) {
                    nodeList.add(node);
                }
            }
            return nodeList;
        }
    }

    public Node findParentNode(String nodeId) {
        Node node = nodeService.getparentId(nodeId);
        return node;
    }

    public List<Node> findChildrenNodes(String nodeId) {
        List<Node> nodeList = new ArrayList<Node>();
        List<Node> nodeList2 = new ArrayList<Node>();
        List<Node> nodeList3 = new ArrayList<Node>();
        if (nodeId != null) {
            nodeList = nodeService.queryChildrenNodeList(nodeId);
            for (Node node : nodeList) {
                nodeList2.add(node);
                nodeList3 = nodeService.queryChildrenNodeList(node.getNodeId());
                if (nodeList3 != null && nodeList3.size() > 0) {
                    nodeList2.addAll(findChildrenNodes(node.getNodeId()));
                }
            }
        }
        return nodeList2;
    }

    public List<Node> findFirshChildrenNodes(String nodeId) {
        List<Node> nodeList = new ArrayList<Node>();
        if (nodeId != null) {
            nodeList = nodeService.queryChildrenNodeList(nodeId);
        }
        return nodeList;
    }
    
    public Node getNode(String nodeId) {
        return nodeService.getNode(nodeId);
    }
    
}
